/*eslint-disable*/

async function FileDataWrite(NeedWriteData, dialogConfigure) {
    let WriterPromiseProcess = new Promise(function (resolve, reject) {
        function operateCore(dialogConfig, readyWriteData) {
            window.showSaveFilePicker(dialogConfig).then(function (FileHandle) {
                FileHandle.createWritable().then(function (WriteHandle) {
                    let WriteConsole = new WritableStream(WriteHandle);
                    let WriteOperate = WriteConsole.getWriter();
                    switch (Object.prototype.toString.call(readyWriteData)) {
                        case "[object Array]":
                            for (let WriteNumber in readyWriteData) {
                                WriteOperate.write(readyWriteData[WriteNumber]).catch(err => {
                                    reject(err);
                                });
                            }
                            break;
                        case "[object String]":
                            WriteOperate.write(readyWriteData).catch(err => {
                                reject(err);
                            });
                            break;
                        case "[object Number]":
                            WriteOperate.write(readyWriteData).catch(err => {
                                reject(err);
                            });
                            break;
                    }
                    WriteOperate.close();
                    WriteOperate.closed.then(function (vbl) {
                        resolve("ok");
                    }).catch(function (vbl) {
                        reject(vbl);
                    });
                }).catch(function (vlx) {
                    reject(vlx)
                });
            }).catch(function (MessageHandle) {
                reject(MessageHandle);
            });
        }
        if (Object.prototype.toString.call(dialogConfigure) !== "[object Object]") {
            dialogConfigure = undefined;
        }
        switch (Object.prototype.toString.call(NeedWriteData)) {
            case "[object Array]":
                if (Array(...NeedWriteData).length > 0) {
                    operateCore(dialogConfigure, NeedWriteData);
                } else {
                    reject("Cannot write an empty array.不能写入空数组");
                }
                break;
            case "[object String]":
                operateCore(dialogConfigure, NeedWriteData);
                break;
            case "[object Number]":
                if (isNaN(NeedWriteData) || !isFinite(NeedWriteData)) {
                    reject("NaN,Finite Data Type Not Support Written.NaN，无穷大数据类型不支持写入");
                } else {
                    operateCore(dialogConfigure, NeedWriteData);
                }
                break;
            case "[object Object]":
                let tempConvert;
                try {
                    tempConvert = JSON.stringify(NeedWriteData);
                    operateCore(dialogConfigure, tempConvert);
                } catch (e) {
                    reject("This Object data Exception,Not Written.此对象数据异常，未写入");
                }
                break;
            default:
                reject("This type of data cannot be recognized and written.这种类型的数据无法识别和写入");
                break;
        }
    });
    let OperateStatus;
    let FinalValue;
    await WriterPromiseProcess.then(value => {
        OperateStatus = true;
        FinalValue = value;
    }).catch(value => {
        OperateStatus = false;
        FinalValue = value
    });
    if (OperateStatus) {
        return FinalValue;
    } else {
        throw FinalValue;
    }
}

export {FileDataWrite};